<!-- 修改用户 -->
<template>
    <div class="app-container">
        <el-form ref="form" :model="user" label-width="80px">
            <el-form-item label="昵称">
                <el-input v-model="user.nickname" style="width:10%"></el-input>
            </el-form-item>
            <el-form-item label="手机号">
                <el-input v-model="user.mobile" style="width:10%"></el-input>
            </el-form-item>
            <el-form-item label="个性签名">
                <el-input type="textarea" :rows="4" placeholder="请输入内容" v-model="user.sign" style="width:40%">
                </el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-select style="width: 10%" v-model="user.sex" placeholder="请选择">
                    <el-option label="不选择" value=""></el-option>
                    <el-option label="女" :value="1"></el-option>
                    <el-option label="男" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="年龄">
                <el-input-number style="width: 10%" v-model="user.age" :min="0"
                    controls-position="right"></el-input-number>
            </el-form-item>
            <el-form-item label="头像">
                <pan-thumb :image="user.avatar" />
                <el-button type="primary" icon="el-icon-upload"
                    style="position: absolute;bottom: 15px;margin-left: 40px;" @click="imagecropperShow = true">
                    Change Avatar
                </el-button>
                <image-cropper v-show="imagecropperShow" field="file" :key="imagecropperKey" :width="300" :height="300"
                    :url="BASE_API" lang-type="en" @close="close" @crop-upload-success="cropSuccess" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

import { getUserById, updateUser, saveUser } from '@/api/ucenter/user'
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
export default {
    //import引入的组件需要注入到对象中才能使用
    components: { ImageCropper, PanThumb },
    props: ['id'],
    data() {
        //这里存放数据
        return {
            user: {},
            imagecropperShow: false,
            imagecropperKey: 0,
            BASE_API: process.env.BASE_API+'/oss/uploadPhoto',
        };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
        onSubmit() {
            if (this.id) {
                updateUser(this.user).then(response => {
                    this.$message({
                        message: '修改成功',
                        type: 'success'
                    });
                })

            } else {
                saveUser(this.user).then(response => {
                    this.$message({
                        message: '保存成功',
                        type: 'success'
                    });
                })
            }
        },
        cropSuccess(resData) {
            this.imagecropperShow = false
            this.imagecropperKey = this.imagecropperKey + 1
            this.user.avatar = resData.url
        },
        close() {
            this.imagecropperShow = false
        }
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {

    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
        if (this.id) {
            getUserById(this.id).then(response => {
                this.user = response.data.user;
            })
        }
    },
    beforeCreate() { }, //生命周期 - 创建之前
    beforeMount() { }, //生命周期 - 挂载之前
    beforeUpdate() { }, //生命周期 - 更新之前
    updated() { }, //生命周期 - 更新之后
    beforeDestroy() { }, //生命周期 - 销毁之前
    destroyed() { }, //生命周期 - 销毁完成
    activated() { }, //如果页面有keep-alive缓存功能，这个函数会触发，页面一旦激活就会触发
}
</script>
<style scoped>

</style>